﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;

namespace ScriptBox
{
    public static class TreeViewItemExtensions
    {
        public static readonly DependencyProperty IsDragOverProperty =
            DependencyProperty.RegisterAttached(
                "IsDragOver",
                typeof(bool),
                typeof(TreeViewItemExtensions),
                new PropertyMetadata(false));

        public static bool GetIsDragOver(TreeViewItem item)
        {
            return (bool)item.GetValue(IsDragOverProperty);
        }

        public static void SetIsDragOver(TreeViewItem item, bool value)
        {
            item.SetValue(IsDragOverProperty, value);
        }

        public static IEnumerable<TreeViewItem> GetAllTreeViewItems(this TreeView treeView)
        {
            for (int i = 0; i < treeView.Items.Count; i++)
            {
                var item = treeView.ItemContainerGenerator.ContainerFromIndex(i) as TreeViewItem;
                if (item != null)
                {
                    yield return item;
                    foreach (var subItem in item.GetAllTreeViewItems())
                    {
                        yield return subItem;
                    }
                }
            }
        }

        public static IEnumerable<TreeViewItem> GetAllTreeViewItems(this TreeViewItem treeViewItem)
        {
            for (int i = 0; i < treeViewItem.Items.Count; i++)
            {
                var item = treeViewItem.ItemContainerGenerator.ContainerFromIndex(i) as TreeViewItem;
                if (item != null)
                {
                    yield return item;
                    foreach (var subItem in item.GetAllTreeViewItems())
                    {
                        yield return subItem;
                    }
                }
            }
        }
    }
}
